library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)


# Chapter 6

### Figure 6.1

# Wave 1
summary(factor(mil_conf.df$Q8))
mil_conf.df$Q8a <- 0
mil_conf.df$Q8a[mil_conf.df$Q8 < 3] <- 1
summary(factor(mil_conf.df$Q8a))

mil_conf.df$DOV_REP <- NA
mil_conf.df$DOV_REP[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_REP[mil_conf.df$DOV_ASSIGNMENT_A == 2] <- 1
summary(factor(mil_conf.df$DOV_REP))

mil_conf.df$DOV_DEM <- NA
mil_conf.df$DOV_DEM[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_DEM[mil_conf.df$DOV_ASSIGNMENT_A == 3] <- 1
summary(factor(mil_conf.df$DOV_DEM))

mil_conf.df$DOV_PART <- NA
mil_conf.df$DOV_PART[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_PART[mil_conf.df$DOV_ASSIGNMENT_A == 4] <- 1
summary(factor(mil_conf.df$DOV_PART))


# Wave 2
summary(factor(df$Q11))
summary(factor(df$Q11_d))

summary(factor(df$P_ASSIGN1))
df$REPUB <- NA
df$REPUB[df$P_ASSIGN1 == 1] <- 0
df$REPUB[df$P_ASSIGN1 == 6] <- 1
summary(factor(df$REPUB))

df$DEMOC <- NA
df$DEMOC[df$P_ASSIGN1 == 1] <- 0
df$DEMOC[df$P_ASSIGN1 == 7] <- 1
summary(factor(df$DEMOC))

df$PARTISAN <- NA
df$PARTISAN[df$P_ASSIGN1 == 1] <- 0
df$PARTISAN[df$P_ASSIGN1 == 8] <- 1
summary(factor(df$PARTISAN))



# Creating weighted survey design objects
w1_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df
  )

w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df
  )

# Wave 1
# dov_assignment_a == 2 - REPUBLICAN OFFICERS
w1r <- svyttest(Q8a~DOV_REP, w1_design)

# dov 3 - DEMOCRAT OFFICERS
w1d <- svyttest(Q8a~DOV_DEM, w1_design)

# dov 4 - Partisan military OFFICERS
w1p <- svyttest(Q8a~DOV_PART, w1_design)


# Wave 2
# P_ASSIGN1 == 6 REPUBLICAN OFFICERS
w2r <- svyttest(Q11_d~REPUB, w2_design)

# P_ASSIGN1 == 7 DEMOCRAT OFFICERS
w2d <- svyttest(Q11_d~DEMOC, w2_design)

# P_ASSIGN1 == 8 Partisan military OFFICERS
w2p <- svyttest(Q11_d~PARTISAN, w2_design)


# Wave 1
pt.est1 <- c(w1r$estimate,
             w1d$estimate,
             w1p$estimate)
ci.low1 <- c(w1r$conf.int[1],
             w1d$conf.int[1],
             w1p$conf.int[1])
ci.high1 <- c(w1r$conf.int[2],
              w1d$conf.int[2],
              w1p$conf.int[2])
plot.labs <- c("Republican\nMilitary",
               "Democrat\nMilitary",
               "Partisan\nMilitary")
df.plot1 <- data.frame(Treatment=plot.labs, estimate=pt.est1, 
                       lower=ci.low1, upper=ci.high1)
df.plot1$Treatment <- factor(df.plot1$Treatment, levels = plot.labs)

plot_w1 <- ggplot()+
  geom_pointrange(data=df.plot1, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = "black")+
  geom_hline(yintercept = 0, color = "red")+
  ylim(-.2,.2)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  ggtitle(label="Wave 1")+
  theme_bw()

# Wave 2
pt.est2 <- c(w2r$estimate,
             w2d$estimate,
             w2p$estimate)
ci.low2 <- c(w2r$conf.int[1],
             w2d$conf.int[1],
             w2p$conf.int[1])
ci.high2 <- c(w2r$conf.int[2],
              w2d$conf.int[2],
              w2p$conf.int[2])
plot.labs <- c("Republican\nMilitary",
               "Democrat\nMilitary",
               "Partisan\nMilitary")
df.plot2 <- data.frame(Treatment=plot.labs, estimate=pt.est2, 
                       lower=ci.low2, upper=ci.high2)
df.plot2$Treatment <- factor(df.plot2$Treatment, levels = plot.labs)

plot_w2 <- ggplot()+
  geom_pointrange(data=df.plot2, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = "black")+
  geom_hline(yintercept = 0, color = "red")+
  ylim(-.2,.2)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  ggtitle(label="Wave 2")+
  theme_bw()

grid.arrange(plot_w1, plot_w2, nrow=1, ncol=2)



##############################################################



### Figure 6.2
# Wave 1
# Binary for confidence
summary(factor(mil_conf.df$Q8))
mil_conf.df$Q8a <- 0
mil_conf.df$Q8a[mil_conf.df$Q8 < 3] <- 1
summary(factor(mil_conf.df$Q8a))

# Mil is going Repub, overall
mil_conf.df$DOV_REP <- NA
mil_conf.df$DOV_REP[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_REP[mil_conf.df$DOV_ASSIGNMENT_A == 2] <- 1
summary(factor(mil_conf.df$DOV_REP))

# MIl is going Dem, overall
mil_conf.df$DOV_DEM <- NA
mil_conf.df$DOV_DEM[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_DEM[mil_conf.df$DOV_ASSIGNMENT_A == 3] <- 1
summary(factor(mil_conf.df$DOV_DEM))

# Mil is partisan, overall
mil_conf.df$DOV_PART <- NA
mil_conf.df$DOV_PART[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_PART[mil_conf.df$DOV_ASSIGNMENT_A == 4] <- 1
summary(factor(mil_conf.df$DOV_PART))


# Wave 2
summary(factor(df$Q11))
summary(factor(df$Q11_d))

# Mil is Repub overall
summary(factor(df$P_ASSIGN1))
df$REPUB <- NA
df$REPUB[df$P_ASSIGN1 == 1] <- 0
df$REPUB[df$P_ASSIGN1 == 6] <- 1
summary(factor(df$REPUB))

# Mil is Dem overall
df$DEMOC <- NA
df$DEMOC[df$P_ASSIGN1 == 1] <- 0
df$DEMOC[df$P_ASSIGN1 == 7] <- 1
summary(factor(df$DEMOC))

# Mil is partisan overall
df$PARTISAN <- NA
df$PARTISAN[df$P_ASSIGN1 == 1] <- 0
df$PARTISAN[df$P_ASSIGN1 == 8] <- 1
summary(factor(df$PARTISAN))

# Drop respondents without party ID
summary(factor(df$party))
df2 <- df[!is.na(df$party),]


# Creating weighted survey design objects
w1_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df
  )

w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )

# Wave 1
# dov_assignment_a == 2 - REPUBLICAN OFFICERS
w1r_d <- svyttest(Q8a~DOV_REP, w1_design[w1_design$variables$party==0])
w1r_i <- svyttest(Q8a~DOV_REP, w1_design[w1_design$variables$party==1])
w1r_r <- svyttest(Q8a~DOV_REP, w1_design[w1_design$variables$party==2])

# dov 3 - DEMOCRAT OFFICERS
w1d_d <- svyttest(Q8a~DOV_DEM, w1_design[w1_design$variables$party==0])
w1d_i <- svyttest(Q8a~DOV_DEM, w1_design[w1_design$variables$party==1])
w1d_r <- svyttest(Q8a~DOV_DEM, w1_design[w1_design$variables$party==2])

# dov 4 - Partisan military OFFICERS
w1p_d <- svyttest(Q8a~DOV_PART, w1_design[w1_design$variables$party==0])
w1p_i <- svyttest(Q8a~DOV_PART, w1_design[w1_design$variables$party==1])
w1p_r <- svyttest(Q8a~DOV_PART, w1_design[w1_design$variables$party==2])


# Wave 2
# P_ASSIGN1 == 6 REPUBLICAN OFFICERS
w2r_d <- svyttest(Q11_d~REPUB, w2_design[w2_design$variables$party==0])
w2r_i <- svyttest(Q11_d~REPUB, w2_design[w2_design$variables$party==1])
w2r_r <- svyttest(Q11_d~REPUB, w2_design[w2_design$variables$party==2])

# P_ASSIGN1 == 7 DEMOCRAT OFFICERS
w2d_d <- svyttest(Q11_d~DEMOC, w2_design[w2_design$variables$party==0])
w2d_i <- svyttest(Q11_d~DEMOC, w2_design[w2_design$variables$party==1])
w2d_r <- svyttest(Q11_d~DEMOC, w2_design[w2_design$variables$party==2])

# P_ASSIGN1 == 8 Partisan military OFFICERS
w2p_d <- svyttest(Q11_d~PARTISAN, w2_design[w2_design$variables$party==0])
w2p_i <- svyttest(Q11_d~PARTISAN, w2_design[w2_design$variables$party==1])
w2p_r <- svyttest(Q11_d~PARTISAN, w2_design[w2_design$variables$party==2])


# Wave 1
pt.est1 <- c(w1r_d$estimate,w1r_i$estimate,w1r_r$estimate,
             w1d_d$estimate,w1d_i$estimate,w1d_r$estimate,
             w1p_d$estimate,w1p_i$estimate,w1p_r$estimate)
ci.low1 <- c(w1r_d$conf.int[1],w1r_i$conf.int[1],w1r_r$conf.int[1],
             w1d_d$conf.int[1],w1d_i$conf.int[1],w1d_r$conf.int[1],
             w1p_d$conf.int[1],w1p_i$conf.int[1],w1p_r$conf.int[1])
ci.high1 <- c(w1r_d$conf.int[2],w1r_i$conf.int[2],w1r_r$conf.int[2],
              w1d_d$conf.int[2],w1d_i$conf.int[2],w1d_r$conf.int[2],
              w1p_d$conf.int[2],w1p_i$conf.int[2],w1p_r$conf.int[2])
plot.labs <- c(" ","Republican\nMilitary","  ",
               "   ","Democrat\nMilitary","    ",
               "     ","Partisan\nMilitary", "      ")
df.plot1 <- data.frame(Treatment=plot.labs, estimate=pt.est1, 
                       lower=ci.low1, upper=ci.high1)
df.plot1$Treatment <- factor(df.plot1$Treatment, levels = plot.labs)

plot_w1 <- ggplot()+
  geom_pointrange(data=df.plot1, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c('blue','gray50','red',
                                        'blue','gray50','red',
                                        'blue','gray50','red'))+
  geom_hline(yintercept = 0, color = "black")+
  ylim(-.4,.4)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  ggtitle(label="Wave 1")+
  theme_bw()

# Wave 2
pt.est2 <- c(w2r_d$estimate,w2r_i$estimate,w2r_r$estimate,
             w2d_d$estimate,w2d_i$estimate,w2d_r$estimate,
             w2p_d$estimate,w2p_i$estimate,w2p_r$estimate)
ci.low2 <- c(w2r_d$conf.int[1],w2r_i$conf.int[1],w2r_r$conf.int[1],
             w2d_d$conf.int[1],w2d_i$conf.int[1],w2d_r$conf.int[1],
             w2p_d$conf.int[1],w2p_i$conf.int[1],w2p_r$conf.int[1])
ci.high2 <- c(w2r_d$conf.int[2],w2r_i$conf.int[2],w2r_r$conf.int[2],
              w2d_d$conf.int[2],w2d_i$conf.int[2],w2d_r$conf.int[2],
              w2p_d$conf.int[2],w2p_i$conf.int[2],w2p_r$conf.int[2])
plot.labs <- c(" ","Republican\nMilitary","  ",
               "   ","Democrat\nMilitary","    ",
               "     ","Partisan\nMilitary", "      ")
df.plot2 <- data.frame(Treatment=plot.labs, estimate=pt.est2, 
                       lower=ci.low2, upper=ci.high2)
df.plot2$Treatment <- factor(df.plot2$Treatment, levels = plot.labs)

plot_w2 <- ggplot()+
  geom_pointrange(data=df.plot2, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c('blue','gray50','red',
                                        'blue','gray50','red',
                                        'blue','gray50','red'))+
  geom_hline(yintercept = 0, color = "black")+
  ylim(-.4,.4)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  ggtitle(label="Wave 2")+
  theme_bw()

grid.arrange(plot_w1, plot_w2, nrow=1, ncol=2)



##############################################################



### Figure 6.3
# Wave 2
summary(factor(df$Q36))
summary(factor(df$P_ASSIGN5))

# Create binary for vote for Trump (2)
summary(factor(df$Q36))
df$Q36T[df$Q36 < 77] <- 0
df$Q36T[df$Q36 == 2] <- 1
summary(factor(df$Q36T))

# Create new variables for assigned condition
df2 <- df
# Mil biden
df2$PA5_mb[df2$P_ASSIGN5 == 1] <- 0
df2$PA5_mb[df2$P_ASSIGN5 == 2] <- 1

# Mil trump
df2$PA5_mt[df2$P_ASSIGN5 == 1] <- 0
df2$PA5_mt[df2$P_ASSIGN5 == 3] <- 1

# Gen Biden
df2$PA5_gb[df2$P_ASSIGN5 == 1] <- 0
df2$PA5_gb[df2$P_ASSIGN5 == 4] <- 1

# Gen Trump
df2$PA5_gt[df2$P_ASSIGN5 == 1] <- 0
df2$PA5_gt[df2$P_ASSIGN5 == 5] <- 1


w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )


# Wave 2
# P_ASSIGN5 == 2 BIDEN
w2mb <- svyttest(Q36T~PA5_mb, w2_design)

# P_ASSIGN5 == 3 TRUMP
w2mt <- svyttest(Q36T~PA5_mt, w2_design)

# P_ASSIGN5 == 4 Generals BIDEN
w2gb <- svyttest(Q36T~PA5_gb, w2_design)

# P_ASSIGN5 == 5 Generals TRUMP
w2gt <- svyttest(Q36T~PA5_gt, w2_design)


# Wave 2
pt.est2 <- c(w2mb$estimate, w2mt$estimate, w2gb$estimate, w2gt$estimate)
ci.low2 <- c(w2mb$conf.int[1],w2mt$conf.int[1], w2gb$conf.int[1],w2gt$conf.int[1])
ci.high2 <- c(w2mb$conf.int[2],w2mt$conf.int[2], w2gb$conf.int[2],w2gt$conf.int[2])
plot.labs <- c('Military:\nBiden','Military:\nTrump','Generals:\nBiden','Generals:\nTrump')
df.plot2 <- data.frame(Treatment=plot.labs, estimate=pt.est2, 
                       lower=ci.low2, upper=ci.high2)
df.plot2$Treatment <- factor(df.plot2$Treatment, levels = plot.labs)

plot_w2 <- ggplot()+
  geom_pointrange(data=df.plot2, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = 'black')+
  geom_hline(yintercept = 0, color = "red")+
  ylim(-.2,.2)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  ggtitle(label="Wave 2")+
  theme_bw()

plot_w2



##############################################################



### Figure 6.4
# Wave 2
summary(factor(df$Q36))
summary(factor(df$P_ASSIGN5))

# Create binary for vote for Trump (2)
summary(factor(df$Q36))
df$Q36T[df$Q36 < 77] <- 0
df$Q36T[df$Q36 == 2] <- 1
summary(factor(df$Q36T))

# Create new variables for assigned condition
df2 <- df

# Mil biden
df2$PA5_mbd[df2$P_ASSIGN5 == 1 & df2$party == 0] <- 0
df2$PA5_mbd[df2$P_ASSIGN5 == 2 & df2$party == 0] <- 1
df2$PA5_mbi[df2$P_ASSIGN5 == 1 & df2$party == 1] <- 0
df2$PA5_mbi[df2$P_ASSIGN5 == 2 & df2$party == 1] <- 1
df2$PA5_mbr[df2$P_ASSIGN5 == 1 & df2$party == 2] <- 0
df2$PA5_mbr[df2$P_ASSIGN5 == 2 & df2$party == 2] <- 1

# Mil trump
df2$PA5_mtd[df2$P_ASSIGN5 == 1 & df2$party == 0] <- 0
df2$PA5_mtd[df2$P_ASSIGN5 == 3 & df2$party == 0] <- 1
df2$PA5_mti[df2$P_ASSIGN5 == 1 & df2$party == 1] <- 0
df2$PA5_mti[df2$P_ASSIGN5 == 3 & df2$party == 1] <- 1
df2$PA5_mtr[df2$P_ASSIGN5 == 1 & df2$party == 2] <- 0
df2$PA5_mtr[df2$P_ASSIGN5 == 3 & df2$party == 2] <- 1

# Gen Biden
df2$PA5_gbd[df2$P_ASSIGN5 == 1 & df2$party == 0] <- 0
df2$PA5_gbd[df2$P_ASSIGN5 == 4 & df2$party == 0] <- 1
df2$PA5_gbi[df2$P_ASSIGN5 == 1 & df2$party == 1] <- 0
df2$PA5_gbi[df2$P_ASSIGN5 == 4 & df2$party == 1] <- 1
df2$PA5_gbr[df2$P_ASSIGN5 == 1 & df2$party == 2] <- 0
df2$PA5_gbr[df2$P_ASSIGN5 == 4 & df2$party == 2] <- 1

# Gen Trump
df2$PA5_gtd[df2$P_ASSIGN5 == 1 & df2$party == 0] <- 0
df2$PA5_gtd[df2$P_ASSIGN5 == 5 & df2$party == 0] <- 1
df2$PA5_gti[df2$P_ASSIGN5 == 1 & df2$party == 1] <- 0
df2$PA5_gti[df2$P_ASSIGN5 == 5 & df2$party == 1] <- 1
df2$PA5_gtr[df2$P_ASSIGN5 == 1 & df2$party == 2] <- 0
df2$PA5_gtr[df2$P_ASSIGN5 == 5 & df2$party == 2] <- 1


w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )


# Wave 2
# P_ASSIGN5 == 2 BIDEN
w2mbd <- svyttest(Q36T~PA5_mbd, w2_design)
w2mbi <- svyttest(Q36T~PA5_mbi, w2_design)
w2mbr <- svyttest(Q36T~PA5_mbr, w2_design)

# P_ASSIGN5 == 3 TRUMP
w2mtd <- svyttest(Q36T~PA5_mtd, w2_design)
w2mti <- svyttest(Q36T~PA5_mti, w2_design)
w2mtr <- svyttest(Q36T~PA5_mtr, w2_design)

# P_ASSIGN5 == 4 Generals BIDEN
w2gbd <- svyttest(Q36T~PA5_gbd, w2_design)
w2gbi <- svyttest(Q36T~PA5_gbi, w2_design)
w2gbr <- svyttest(Q36T~PA5_gbr, w2_design)

# P_ASSIGN5 == 5 Generals TRUMP
w2gtd <- svyttest(Q36T~PA5_gtd, w2_design)
w2gti <- svyttest(Q36T~PA5_gti, w2_design)
w2gtr <- svyttest(Q36T~PA5_gtr, w2_design)

# Wave 2
pt.est2 <- c(w2mbd$estimate, w2mbi$estimate, w2mbr$estimate, 
             w2mtd$estimate, w2mti$estimate, w2mtr$estimate, 
             w2gbd$estimate, w2gbi$estimate, w2gbr$estimate, 
             w2gtd$estimate, w2gti$estimate, w2gtr$estimate)
ci.low2 <- c(w2mbd$conf.int[1], w2mbi$conf.int[1], w2mbr$conf.int[1], 
             w2mtd$conf.int[1], w2mti$conf.int[1], w2mtr$conf.int[1], 
             w2gbd$conf.int[1], w2gbi$conf.int[1], w2gbr$conf.int[1], 
             w2gtd$conf.int[1], w2gti$conf.int[1], w2gtr$conf.int[1])
ci.high2 <- c(w2mbd$conf.int[2], w2mbi$conf.int[2], w2mbr$conf.int[2], 
              w2mtd$conf.int[2], w2mti$conf.int[2], w2mtr$conf.int[2], 
              w2gbd$conf.int[2], w2gbi$conf.int[2], w2gbr$conf.int[2], 
              w2gtd$conf.int[2], w2gti$conf.int[2], w2gtr$conf.int[2])
plot.labs <- c('','Military:\nBiden',' ',
               '  ','Military:\nTrump','   ',
               '    ','Generals:\nBiden','     ',
               '      ','Generals:\nTrump','       ')
df.plot2 <- data.frame(Treatment=plot.labs, estimate=pt.est2, 
                       lower=ci.low2, upper=ci.high2)
df.plot2$Treatment <- factor(df.plot2$Treatment, levels = plot.labs)

plot_w2 <- ggplot()+
  geom_pointrange(data=df.plot2, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c('blue','gray50','red',
                                        'blue','gray50','red',
                                        'blue','gray50','red',
                                        'blue','gray50','red'))+
  geom_hline(yintercept = 0, color = "black")+
  ylim(-.275,.175)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  ggtitle(label="Wave 2")+
  theme_bw()

plot_w2



##############################################################



### Figure 6.5
# Wave 1

# Create binaries for treatement groups and controls
# Trump Politicizes
mil_conf.df$q42_tp[mil_conf.df$DOV_ASSIGNMENT_C == 2] <- 0
mil_conf.df$q42_tp[mil_conf.df$DOV_ASSIGNMENT_C == 3] <- 1
# Dem Politicizes
mil_conf.df$q42_dep[mil_conf.df$DOV_ASSIGNMENT_C == 2] <- 0
mil_conf.df$q42_dep[mil_conf.df$DOV_ASSIGNMENT_C == 4] <- 1
# Mil SUpports
mil_conf.df$q42_ms[mil_conf.df$DOV_ASSIGNMENT_C == 2] <- 0
mil_conf.df$q42_ms[mil_conf.df$DOV_ASSIGNMENT_C == 5] <- 1
# Mil opposes
mil_conf.df$q42_mo[mil_conf.df$DOV_ASSIGNMENT_C == 2] <- 0
mil_conf.df$q42_mo[mil_conf.df$DOV_ASSIGNMENT_C == 6] <- 1

# COde agreement with Trump declaration
mil_conf.df$Q42A2[mil_conf.df$Q42A < 77] <- 0
mil_conf.df$Q42A2[mil_conf.df$Q42A < 3] <- 1

# Drop NA
mil_conf.dfa <- mil_conf.df[!is.na(mil_conf.df$Q42A2),]

# Creating weighted survey design objects
w1_design_a <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.dfa
  )

# Wave 1
# Trump Politicizes
w1t_d <- svyttest(Q42A2~q42_tp, w1_design_a[w1_design_a$variables$party==0])
w1t_i <- svyttest(Q42A2~q42_tp, w1_design_a[w1_design_a$variables$party==1])
w1t_r <- svyttest(Q42A2~q42_tp, w1_design_a[w1_design_a$variables$party==2])

# Dem Politicizes
w1d_d <- svyttest(Q42A2~q42_dep, w1_design_a[w1_design_a$variables$party==0])
w1d_i <- svyttest(Q42A2~q42_dep, w1_design_a[w1_design_a$variables$party==1])
w1d_r <- svyttest(Q42A2~q42_dep, w1_design_a[w1_design_a$variables$party==2])

# Mil Supports
w1ms_d <- svyttest(Q42A2~q42_ms, w1_design_a[w1_design_a$variables$party==0])
w1ms_i <- svyttest(Q42A2~q42_ms, w1_design_a[w1_design_a$variables$party==1])
w1ms_r <- svyttest(Q42A2~q42_ms, w1_design_a[w1_design_a$variables$party==2])

# Mil Opposes
w1mo_d <- svyttest(Q42A2~q42_mo, w1_design_a[w1_design_a$variables$party==0])
w1mo_i <- svyttest(Q42A2~q42_mo, w1_design_a[w1_design_a$variables$party==1])
w1mo_r <- svyttest(Q42A2~q42_mo, w1_design_a[w1_design_a$variables$party==2])



# Wave 1
pt.est1 <- c(w1t_d$estimate, w1t_i$estimate, w1t_r$estimate,
             w1d_d$estimate, w1d_i$estimate, w1d_r$estimate,
             w1ms_d$estimate, w1ms_i$estimate, w1ms_r$estimate,
             w1mo_d$estimate, w1mo_i$estimate, w1mo_r$estimate)
ci.low1 <- c(w1t_d$conf.int[1], w1t_i$conf.int[1], w1t_r$conf.int[1],
             w1d_d$conf.int[1], w1d_i$conf.int[1], w1d_r$conf.int[1],
             w1ms_d$conf.int[1], w1ms_i$conf.int[1], w1ms_r$conf.int[1],
             w1mo_d$conf.int[1], w1mo_i$conf.int[1], w1mo_r$conf.int[1])
ci.high1 <- c(w1t_d$conf.int[2], w1t_i$conf.int[2], w1t_r$conf.int[2],
              w1d_d$conf.int[2], w1d_i$conf.int[2], w1d_r$conf.int[2],
              w1ms_d$conf.int[2], w1ms_i$conf.int[2], w1ms_r$conf.int[2],
              w1mo_d$conf.int[2], w1mo_i$conf.int[2], w1mo_r$conf.int[2])
plot.labs <- c(" ","Trump\nPoliticizes","  ",
               "   ","Democrat\nPoliticizes","    ",
               "     ","Military\nSupports", "      ",
               "       ","Military\nOpposes", "        ")
df.plot1 <- data.frame(Treatment=plot.labs, estimate=pt.est1, 
                       lower=ci.low1, upper=ci.high1)
df.plot1$Treatment <- factor(df.plot1$Treatment, levels = plot.labs)

plot_w1 <- ggplot()+
  geom_pointrange(data=df.plot1, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c('blue','gray50','red',
                                        'blue','gray50','red',
                                        'blue','gray50','red',
                                        'blue','gray50','red'))+
  geom_hline(yintercept = 0, color = "black")+
  ylim(-.3,.25)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  theme_bw()

plot_w1



##############################################################



### Figure 6.6
# Wave 1

# Create binaries for treatement groups and controls
# Trump Politicizes
mil_conf.df$q42_tp[mil_conf.df$DOV_ASSIGNMENT_C == 2] <- 0
mil_conf.df$q42_tp[mil_conf.df$DOV_ASSIGNMENT_C == 3] <- 1
# Dem Politicizes
mil_conf.df$q42_dep[mil_conf.df$DOV_ASSIGNMENT_C == 2] <- 0
mil_conf.df$q42_dep[mil_conf.df$DOV_ASSIGNMENT_C == 4] <- 1
# Mil SUpports
mil_conf.df$q42_ms[mil_conf.df$DOV_ASSIGNMENT_C == 2] <- 0
mil_conf.df$q42_ms[mil_conf.df$DOV_ASSIGNMENT_C == 5] <- 1
# Mil opposes
mil_conf.df$q42_mo[mil_conf.df$DOV_ASSIGNMENT_C == 2] <- 0
mil_conf.df$q42_mo[mil_conf.df$DOV_ASSIGNMENT_C == 6] <- 1

# Code agreement with Trust the military
mil_conf.df$Q42F2[mil_conf.df$Q42F < 77] <- 0
mil_conf.df$Q42F2[mil_conf.df$Q42F < 3] <- 1

# Drop NA
mil_conf.dff <- mil_conf.df[!is.na(mil_conf.df$Q42F2),]

# Creating weighted survey design objects
w1_design_f <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.dff
  )

# Wave 1
# Trump Politicizes
w1t_d <- svyttest(Q42F2~q42_tp, w1_design_f[w1_design_f$variables$party==0])
w1t_i <- svyttest(Q42F2~q42_tp, w1_design_f[w1_design_f$variables$party==1])
w1t_r <- svyttest(Q42F2~q42_tp, w1_design_f[w1_design_f$variables$party==2])

# Dem Politicizes
w1d_d <- svyttest(Q42F2~q42_dep, w1_design_f[w1_design_f$variables$party==0])
w1d_i <- svyttest(Q42F2~q42_dep, w1_design_f[w1_design_f$variables$party==1])
w1d_r <- svyttest(Q42F2~q42_dep, w1_design_f[w1_design_f$variables$party==2])

# Mil Supports
w1ms_d <- svyttest(Q42F2~q42_ms, w1_design_f[w1_design_f$variables$party==0])
w1ms_i <- svyttest(Q42F2~q42_ms, w1_design_f[w1_design_f$variables$party==1])
w1ms_r <- svyttest(Q42F2~q42_ms, w1_design_f[w1_design_f$variables$party==2])

# Mil Opposes
w1mo_d <- svyttest(Q42F2~q42_mo, w1_design_f[w1_design_f$variables$party==0])
w1mo_i <- svyttest(Q42F2~q42_mo, w1_design_f[w1_design_f$variables$party==1])
w1mo_r <- svyttest(Q42F2~q42_mo, w1_design_f[w1_design_f$variables$party==2])



# Wave 1
pt.est1 <- c(w1t_d$estimate, w1t_i$estimate, w1t_r$estimate,
             w1d_d$estimate, w1d_i$estimate, w1d_r$estimate,
             w1ms_d$estimate, w1ms_i$estimate, w1ms_r$estimate,
             w1mo_d$estimate, w1mo_i$estimate, w1mo_r$estimate)
ci.low1 <- c(w1t_d$conf.int[1], w1t_i$conf.int[1], w1t_r$conf.int[1],
             w1d_d$conf.int[1], w1d_i$conf.int[1], w1d_r$conf.int[1],
             w1ms_d$conf.int[1], w1ms_i$conf.int[1], w1ms_r$conf.int[1],
             w1mo_d$conf.int[1], w1mo_i$conf.int[1], w1mo_r$conf.int[1])
ci.high1 <- c(w1t_d$conf.int[2], w1t_i$conf.int[2], w1t_r$conf.int[2],
              w1d_d$conf.int[2], w1d_i$conf.int[2], w1d_r$conf.int[2],
              w1ms_d$conf.int[2], w1ms_i$conf.int[2], w1ms_r$conf.int[2],
              w1mo_d$conf.int[2], w1mo_i$conf.int[2], w1mo_r$conf.int[2])
plot.labs <- c(" ","Trump\nPoliticizes","  ",
               "   ","Democrat\nPoliticizes","    ",
               "     ","Military\nSupports", "      ",
               "       ","Military\nOpposes", "        ")
df.plot1 <- data.frame(Treatment=plot.labs, estimate=pt.est1, 
                       lower=ci.low1, upper=ci.high1)
df.plot1$Treatment <- factor(df.plot1$Treatment, levels = plot.labs)

plot_w1 <- ggplot()+
  geom_pointrange(data=df.plot1, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c('blue','gray50','red',
                                        'blue','gray50','red',
                                        'blue','gray50','red',
                                        'blue','gray50','red'))+
  geom_hline(yintercept = 0, color = "black")+
  ylim(-.25,.3)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  theme_bw()
plot_w1



##############################################################



### Figure 6.7
# Wave 2

# Create binaries for treatment group and controls for Q25A
# P_ASSIGN3A (0 control, 1 oppose, 2 support)
df$Q25A_opp[df$P_ASSIGN3A == 1] <- 0
df$Q25A_opp[df$P_ASSIGN3A == 2] <- 1

df$Q25A_sup[df$P_ASSIGN3A == 1] <- 0
df$Q25A_sup[df$P_ASSIGN3A == 3] <- 1

# Code agreement with Insurrection Act
df$Q25A2[df$Q25A < 77] <- 0
df$Q25A2[df$Q25A < 3] <- 1
df2 <- df[!is.na(df$Q25A2),]
summary(factor(df2$Q25A2))

# drop NA
df2_opp <- df2[!is.na(df2$Q25A_opp),]
df2_sup <- df2[!is.na(df2$Q25A_sup),]
df2_sup <- df2_sup[!is.na(df2_sup$party),]

# Creating weighted survey design objects
w2_design_opp <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2_opp
  )

w2_design_sup <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2_sup
  )


# Oppose
w2o_d <- svyttest(Q25A2~Q25A_opp, w2_design_opp[w2_design_opp$variables$party==0])
w2o_r <- svyttest(Q25A2~Q25A_opp, w2_design_opp[w2_design_opp$variables$party==2])

# Supports
w2s_d <- svyttest(Q25A2~Q25A_sup, w2_design_sup[w2_design_sup$variables$party==0])
w2s_r <- svyttest(Q25A2~Q25A_sup, w2_design_sup[w2_design_sup$variables$party==2])


# Wave 2
pt.est1 <- c(w2o_d$estimate,1,w2o_r$estimate,1,1,1,w2s_d$estimate,1,
             w2s_r$estimate)
ci.low1 <- c(w2o_d$conf.int[1],.9,w2o_r$conf.int[1],.9,.9,.9,w2s_d$conf.int[1],.9,
             w2s_r$conf.int[1])
ci.high1 <- c(w2o_d$conf.int[2],1.1, w2o_r$conf.int[2], 1.1,1.1,1.1, w2s_d$conf.int[2],1.1,
              w2s_r$conf.int[2])
plot.labs <- c(" ","Oppose","","  ","    ","     ","           ",
               "Support","        ")
df.plot1 <- data.frame(Treatment=plot.labs, estimate=pt.est1, 
                       lower=ci.low1, upper=ci.high1)
df.plot1$Treatment <- factor(df.plot1$Treatment, levels = plot.labs)

plot_w2 <- ggplot()+
  geom_pointrange(data=df.plot1, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c('blue','gray10','red','gray10','gray10','gray10',
                                        'blue','gray10','red'))+
  geom_hline(yintercept = 0, color = "black")+
  ylim(-.2,.2)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  theme_bw()
plot_w2



##############################################################



### Figure 6.8
# Wave 2

# Create binaries for Q20DD2 - Active duty Gen/Adm to crit President
summary(factor(df$Q20DD))

df$Q20DD2[df$Q20DD < 3] <- 1
df$Q20DD2[df$Q20DD > 2 & df$Q20DD < 77] <- 0
summary(factor(df$Q20DD2))

# P_Q20 is Criticize conditions: 1 = Crit Trump; 2 = Crit Obama; 3 = Crit POTUS
summary(factor(df$P_Q20))

# Drop NA
df2 <- df[!is.na(df$Q20DD2),]
df2 <- df2[!is.na(df2$party),]

# Creating weighted survey design objects
w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )

# Wave 2
# Criticize Trump
w2t_d <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==1 & w2_design$variables$party==0],method='me',df=degf(w2_design))
w2t_i <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==1 & w2_design$variables$party==1],method='me',df=degf(w2_design))
w2t_r <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==1 & w2_design$variables$party==2],method='me',df=degf(w2_design))

# Criticize Obama
w2o_d <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==2 & w2_design$variables$party==0],method='me',df=degf(w2_design))
w2o_i <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==2 & w2_design$variables$party==1],method='me',df=degf(w2_design))
w2o_r <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==2 & w2_design$variables$party==2],method='me',df=degf(w2_design))

# Criticize POTUS
w2p_d <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==3 & w2_design$variables$party==0],method='me',df=degf(w2_design))
w2p_i <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==3 & w2_design$variables$party==1],method='me',df=degf(w2_design))
w2p_r <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==3 & w2_design$variables$party==2],method='me',df=degf(w2_design))




# Wave 2
pt.est1 <- c(w2t_d[1],w2t_i[1],w2t_r[1],1,
             w2o_d[1],w2o_i[1],w2o_r[1],1,
             w2p_d[1],w2p_i[1],w2p_r[1])
ci.low1 <- c(attr(w2t_d,'ci')[1],attr(w2t_i,'ci')[1],attr(w2t_r,'ci')[1],.99,
             attr(w2o_d,'ci')[1],attr(w2o_i,'ci')[1],attr(w2o_r,'ci')[1],.99,
             attr(w2p_d,'ci')[1],attr(w2p_i,'ci')[1],attr(w2p_r,'ci')[1])
ci.high1 <- c(attr(w2t_d,'ci')[2],attr(w2t_i,'ci')[2],attr(w2t_r,'ci')[2],1.1,
              attr(w2o_d,'ci')[2],attr(w2o_i,'ci')[2],attr(w2o_r,'ci')[2],1.1,
              attr(w2p_d,'ci')[2],attr(w2p_i,'ci')[2],attr(w2p_r,'ci')[2])
plot.labs <- c(" ","Criticize\nTrump","  ","        ",
               "   ","Criticize\nObama","    ","          ",
               "     ","Criticize\nPOTUS", "      ")
df.plot1 <- data.frame(Treatment=plot.labs, estimate=pt.est1, 
                       lower=ci.low1, upper=ci.high1)
df.plot1$Treatment <- factor(df.plot1$Treatment, levels = plot.labs)

plot_w2 <- ggplot()+
  geom_pointrange(data=df.plot1, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c('blue','gray50','red','gray10',
                                        'blue','gray50','red','gray10',
                                        'blue','gray50','red'))+
  geom_hline(yintercept = 0, color = "black")+
  ylim(.2,.75)+
  ylab("Respondent Agreement (%)")+
  xlab("Treatment Condition")+
  theme_bw()
plot_w2



##############################################################



### Figure 6.9
# Wave 2

# Create binaries for Q20DD2 - Active duty Gen/Adm to crit President
summary(factor(df$Q20DD))

df$Q20DD2[df$Q20DD < 3] <- 1
df$Q20DD2[df$Q20DD > 2 & df$Q20DD < 77] <- 0
summary(factor(df$Q20DD2))

# P_Q20 is Criticize conditions: 1 = Crit Trump; 2 = Crit Obama; 3 = Crit POTUS
summary(factor(df$P_Q20))

# Create Confidence binary - 0 not confident; 1 confident
summary(factor(df$Q11))
df$Q11T <- NA
df$Q11T[df$Q11 < 3] <- 1
df$Q11T[df$Q11 > 2 & df$Q11 < 77] <- 0

# Drop NA
df2 <- df[!is.na(df$Q20DD2),]
df2 <- df2[!is.na(df2$party),]
df2 <- df2[!is.na(df2$Q11T),]

# Creating weighted survey design objects
w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )


# Wave 2 - Not Confident
# Criticize Trump
w2t_d_n <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==1 & w2_design$variables$party==0 & w2_design$variables$Q11T == 0],method='me',df=degf(w2_design))
w2t_i_n <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==1 & w2_design$variables$party==1 & w2_design$variables$Q11T == 0],method='me',df=degf(w2_design))
w2t_r_n <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==1 & w2_design$variables$party==2 & w2_design$variables$Q11T == 0],method='me',df=degf(w2_design))

# Criticize Obama
w2o_d_n <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==2 & w2_design$variables$party==0 & w2_design$variables$Q11T == 0],method='me',df=degf(w2_design))
w2o_i_n <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==2 & w2_design$variables$party==1 & w2_design$variables$Q11T == 0],method='me',df=degf(w2_design))
w2o_r_n <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==2 & w2_design$variables$party==2 & w2_design$variables$Q11T == 0],method='me',df=degf(w2_design))

# Criticize POTUS
w2p_d_n <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==3 & w2_design$variables$party==0 & w2_design$variables$Q11T == 0],method='me',df=degf(w2_design))
w2p_i_n <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==3 & w2_design$variables$party==1 & w2_design$variables$Q11T == 0],method='me',df=degf(w2_design))
w2p_r_n <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==3 & w2_design$variables$party==2 & w2_design$variables$Q11T == 0],method='me',df=degf(w2_design))

# Wave 2 - Confident
# Criticize Trump
w2t_d_c <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==1 & w2_design$variables$party==0 & w2_design$variables$Q11T == 1],method='me',df=degf(w2_design))
w2t_i_c <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==1 & w2_design$variables$party==1 & w2_design$variables$Q11T == 1],method='me',df=degf(w2_design))
w2t_r_c <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==1 & w2_design$variables$party==2 & w2_design$variables$Q11T == 1],method='me',df=degf(w2_design))

# Criticize Obama
w2o_d_c <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==2 & w2_design$variables$party==0 & w2_design$variables$Q11T == 1],method='me',df=degf(w2_design))
w2o_i_c <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==2 & w2_design$variables$party==1 & w2_design$variables$Q11T == 1],method='me',df=degf(w2_design))
w2o_r_c <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==2 & w2_design$variables$party==2 & w2_design$variables$Q11T == 1],method='me',df=degf(w2_design))

# Criticize POTUS
w2p_d_c <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==3 & w2_design$variables$party==0 & w2_design$variables$Q11T == 1],method='me',df=degf(w2_design))
w2p_i_c <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==3 & w2_design$variables$party==1 & w2_design$variables$Q11T == 1],method='me',df=degf(w2_design))
w2p_r_c <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==3 & w2_design$variables$party==2 & w2_design$variables$Q11T == 1],method='me',df=degf(w2_design))


# Wave 2 - Not confident
pt.est1 <- c(w2t_d_n[1],w2t_i_n[1],w2t_r_n[1],2,
             w2o_d_n[1],w2o_i_n[1],w2o_r_n[1],2,
             w2p_d_n[1],w2p_i_n[1],w2p_r_n[1])
ci.low1 <- c(attr(w2t_d_n,'ci')[1],attr(w2t_i_n,'ci')[1],attr(w2t_r_n,'ci')[1],1.99,
             attr(w2o_d_n,'ci')[1],attr(w2o_i_n,'ci')[1],attr(w2o_r_n,'ci')[1],1.99,
             attr(w2p_d_n,'ci')[1],attr(w2p_i_n,'ci')[1],attr(w2p_r_n,'ci')[1])
ci.high1 <- c(attr(w2t_d_n,'ci')[2],attr(w2t_i_n,'ci')[2],attr(w2t_r_n,'ci')[2],2.1,
              attr(w2o_d_n,'ci')[2],attr(w2o_i_n,'ci')[2],attr(w2o_r_n,'ci')[2],2.1,
              attr(w2p_d_n,'ci')[2],attr(w2p_i_n,'ci')[2],attr(w2p_r_n,'ci')[2])
plot.labs <- c(" ","Criticize\nTrump","  ","        ",
               "   ","Criticize\nObama","    ","          ",
               "     ","Criticize\nPOTUS", "      ")
df.plot1 <- data.frame(Treatment=plot.labs, estimate=pt.est1, 
                       lower=ci.low1, upper=ci.high1)
df.plot1$Treatment <- factor(df.plot1$Treatment, levels = plot.labs)

plot_w2n <- ggplot()+
  geom_pointrange(data=df.plot1, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c('blue','gray50','red','gray10',
                                        'blue','gray50','red','gray10',
                                        'blue','gray50','red'))+
  geom_hline(yintercept = 0, color = "black")+
  ylim(0,0.95)+
  ylab("Respondent Agreement (%)")+
  xlab("")+
  ggtitle("Not Confident")+
  theme_bw()


# Wave 2 - Confident
pt.est1 <- c(w2t_d_c[1],w2t_i_c[1],w2t_r_c[1],2,
             w2o_d_c[1],w2o_i_c[1],w2o_r_c[1],2,
             w2p_d_c[1],w2p_i_c[1],w2p_r_c[1])
ci.low1 <- c(attr(w2t_d_c,'ci')[1],attr(w2t_i_c,'ci')[1],attr(w2t_r_c,'ci')[1],1.99,
             attr(w2o_d_c,'ci')[1],attr(w2o_i_c,'ci')[1],attr(w2o_r_c,'ci')[1],1.99,
             attr(w2p_d_c,'ci')[1],attr(w2p_i_c,'ci')[1],attr(w2p_r_c,'ci')[1])
ci.high1 <- c(attr(w2t_d_c,'ci')[2],attr(w2t_i_c,'ci')[2],attr(w2t_r_c,'ci')[2],2.1,
              attr(w2o_d_c,'ci')[2],attr(w2o_i_c,'ci')[2],attr(w2o_r_c,'ci')[2],2.1,
              attr(w2p_d_c,'ci')[2],attr(w2p_i_c,'ci')[2],attr(w2p_r_c,'ci')[2])
plot.labs <- c(" ","Criticize\nTrump","  ","        ",
               "   ","Criticize\nObama","    ","          ",
               "     ","Criticize\nPOTUS", "      ")
df.plot1 <- data.frame(Treatment=plot.labs, estimate=pt.est1, 
                       lower=ci.low1, upper=ci.high1)
df.plot1$Treatment <- factor(df.plot1$Treatment, levels = plot.labs)

plot_w2c <- ggplot()+
  geom_pointrange(data=df.plot1, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c('blue','gray50','red','gray10',
                                        'blue','gray50','red','gray10',
                                        'blue','gray50','red'))+
  geom_hline(yintercept = 0, color = "black")+
  ylim(0,.95)+
  ylab("Respondent Agreement (%)")+
  xlab("")+
  ggtitle("Confident")+
  theme_bw()

grid.arrange(plot_w2n, plot_w2c, nrow=1, ncol=2)

